/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.tesoreria.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import org.cip.esymons.general.util.ControladorAbstracto;
import org.cip.esymons.tesoreria.be.TipoMoneda;
import org.cip.esymons.tesoreria.beans.TipoMonedaController;

/**
 *
 * @author Administrador
 */
public class TipoMonedaConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) throws ConverterException {
        if (string == null ) {
            return null;
        }else{
           try {
                Long id = new Long(string);
                TipoMonedaController controller = (TipoMonedaController) ControladorAbstracto.getBean("tipoMonedaController");
                return controller.buscarPorCodigo(id);
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
        }

        }
       
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object object) throws ConverterException {
        if (object == null) {
            return null;
        }
        if (object instanceof TipoMoneda) {
            TipoMoneda o = (TipoMoneda) object;
            return "" + o.getTmoCodigo();
        } else {
           return null;
        }
    }
}
